package com.Dao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.Connection;
import java.sql.DriverManager;

public class MariaDataSource {

    private String UserName;
    private String Password;
    private String JDBCURL;
    private String DriverClass;

    public String getUserName() {
        return UserName;
    }

    public String getPassword() {
        return Password;
    }

    public String getJDBCURL() {
        return JDBCURL;
    }

    public String getDriverClass() {
        return DriverClass;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public void setPassword(String password) {
        Password = password;
    }

    public void setJDBCURL(String JDBCURL) {
        this.JDBCURL = JDBCURL;
    }

    public void setDriverClass(String driverClass) {
        DriverClass = driverClass;
    }

    public static Connection getConnection() throws Exception {
        Connection c;
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        MariaDataSource dataSource = applicationContext.getBean("datasource", MariaDataSource.class);
        Class.forName(dataSource.getDriverClass());
        c = DriverManager.getConnection
                (
                        dataSource.getJDBCURL(),
                        dataSource.getUserName(),
                        dataSource.getPassword()
                );
        return c;
    }
}
